package com.Ngbussiness.dao.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.Ngbussiness.dao.membersDao;
import com.Ngbussiness.util.Calenda;
import com.Ngbussiness.util.DatabaseAccess;
import com.Ngbussiness.util.EasyMapsManager;
import com.Ngbussiness.util.MD5;


public class membersDaoImpl implements membersDao{
	public boolean Logon(String name,String password,String mobel,String sex){
		DatabaseAccess dao=new DatabaseAccess();
		Calenda C=new Calenda();
		String now=C.getNowTime().toString();
		String sql="insert into members (name,password,role_id,uptime,mobel," +
				"logintime,is_useful,is_email,is_mobel,wait_delete,province,city,zone,is_recognise,is_card_ok,sex) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		dao.setPreparedParameter(name);
		dao.setPreparedParameter(password);
		//ע���û������Ϊ2
		dao.setPreparedParameter(2);
		dao.setPreparedParameter(now);
		dao.setPreparedParameter(mobel);
		dao.setPreparedParameter(now);
		dao.setPreparedParameter(1);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(sex);
		boolean b=dao.executeInsert(sql)>0?true:false;
		return b;
	}
	//��ѯ�Ƿ�����ͬ���ֻ�ţ���ͬ������ע��
	public List samename(String name){
		DatabaseAccess dao=new DatabaseAccess();
		String sql="select * from members where mobel=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(name);
			List list = easyMapsManager.executeQuery(sql);
			databaseAccess.close();
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List login (String name,String password){
		DatabaseAccess dao=new DatabaseAccess();
		String sql="select m.Id,m.name,m.sex,m.mobel,m.email,m.province,m.city,m.bank_card,m.logintime,m.recognise_id,m.name1," +
				"m.name2,m.name3 from memberview m where m.mobel=? And m.password=? AND is_useful=1;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(name);
			easyMapsManager.setPreparedParameter(password);
			List list = easyMapsManager.executeQuery(sql);
			if(list.size()>0){
				HashMap adu=(HashMap)list.get(0);
				Calenda C=new Calenda();
				String curdate=C.getNowTime().toString();
				String Id=adu.get("Id").toString();
				adu.put("logintime",curdate);
				String sql2="update members set logintime=? where Id=?";
				dao.setPreparedParameter(curdate);
				dao.setPreparedParameter(Id);
				dao.executeUpdate(sql2);
			}
			databaseAccess.close();
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public String verifyBusiness(String userid) {
		// TODO Auto-generated method stub
		DatabaseAccess dao=new DatabaseAccess();
		String sql="select recognise_id from members where Id=?";
		
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(userid);
			List list = easyMapsManager.executeQuery(sql);
			
			if(list.size()>0){
				HashMap adu=(HashMap)list.get(0);
				
				if(adu.get("recognise_id")==null){
					databaseAccess.close();
					return null;
				}
				else{
					databaseAccess.close();
					return adu.get("recognise_id").toString();
				}
					
			}
			databaseAccess.close();	
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	public boolean recharge(String userid, String money) {
		// TODO Auto-generated method stub
		DatabaseAccess dao=new DatabaseAccess();
		DatabaseAccess dao2=new DatabaseAccess();
		Calenda C=new Calenda();
		String now=C.getNowTime().toString();
		
		String sql = "update members set yongjin=yongjin+? where Id=?";
		String sql2 = "insert into recharge(member_id,charge_time,charge_money) values(?,?,?)";
		try{
			
			dao.setPreparedParameter(money);
			dao.setPreparedParameter(userid);
			dao.executeUpdate(sql);
			
		
			dao2.setPreparedParameter(userid);
			dao2.setPreparedParameter(now);
			dao2.setPreparedParameter(money);
			boolean b=dao2.executeInsert(sql2)>0?true:false;
			
			return b;
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public String showMoney(String userid) {
		// TODO Auto-generated method stub
		DatabaseAccess dao=new DatabaseAccess();
		String sql="select yongjin from members where Id=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(userid);
			List list = easyMapsManager.executeQuery(sql);
			if(list.size()>0){
				HashMap adu=(HashMap)list.get(0);
				return adu.get("yongjin").toString();
					
			}
			databaseAccess.close();	
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return "0";
	}
	
	public String checkqrm(int userid, String time2){
		DatabaseAccess dao=new DatabaseAccess();
		String sql="select m.last_time,m.num from members m where m.is_useful=1 and m.Id=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(userid);
			List list = easyMapsManager.executeQuery(sql);
			if(list.size()>0){
				HashMap adu=(HashMap)list.get(0);
				String last_time=adu.get("last_time").toString();
				int num=Integer.parseInt(adu.get("num").toString());
				System.out.println("1"+last_time.equals(time2));
				if(last_time.equals(time2)&&num<5){
					return "b1";
				}
				else if(last_time.equals(time2)&&num==5)
				{
					return "f1";
				}
				else{
					String sql3="update members set last_time=?,num=0 where Id=?";
					dao.setPreparedParameter(time2);
					dao.setPreparedParameter(userid);
					dao.executeUpdate(sql3);
					return "b2";
				}
			}
			databaseAccess.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return "f2";
	}
	
	 public JSONArray paymoney(int userid){
		 JSONArray messagelist = new JSONArray();
			JSONObject message = new JSONObject();
			String sql = "select yj.price,yj.num,yj.start_time,yj.businessname from yongjin yj where yj.member_id=?;";
			try {
				DatabaseAccess databaseAccess = new DatabaseAccess(false);
				EasyMapsManager easyMapsManager = new EasyMapsManager(
						databaseAccess);
				easyMapsManager.setPreparedParameter(userid);
				List list = easyMapsManager.executeQuery(sql);
				for(int i=0;i<list.size();i++){
					HashMap adu = (HashMap) list.get(i);
					double price=Double.parseDouble((adu.get("price").toString()));
					String businessname=adu.get("businessname").toString();
					if(businessname==null){
						businessname="null";
					}
					String start_time=adu.get("start_time").toString();
					int num=Integer.parseInt(adu.get("num").toString());
					double zgmoney=price*num;
					message.put("time",start_time);
					message.put("source",businessname);
					message.put("money", zgmoney);
					message.put("category",0);
					messagelist.add(message);
					System.out.println("1"+message);
				}
				List list2 = new ArrayList();
						try {
							String sql2 = "select uc.pay,uc.add_time,yj.businessname from " +
									"`use-coupon` uc left join yongjin yj on yj.Id=uc.yongjin_id where uc.member_id=? and uc.is_use=1;";
							DatabaseAccess databaseAccess1 = new DatabaseAccess(
									false);
							EasyMapsManager easyMapsManager1 = new EasyMapsManager(
									databaseAccess1);
							easyMapsManager1.setPreparedParameter(userid);
							list2 = easyMapsManager1.executeQuery(sql2);
							for(int i=0;i<list2.size();i++){
								HashMap adu = (HashMap) list2.get(i);
								double pay=Double.parseDouble(adu.get("pay").toString());
								String businessname=adu.get("businessname").toString();
								String add_time=adu.get("add_time").toString();
								message.put("time",add_time);
								message.put("source",businessname);
								message.put("money", pay);
								message.put("category",1);
								System.out.println("2"+message);
								messagelist.add(message);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
				databaseAccess.close();
				return messagelist;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
	 }
	 
//	 public List samename1(String username){
//			String sql="select Id from members where name like ?;";
//			try{
//				DatabaseAccess databaseAccess = new DatabaseAccess(false);
//				EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
//				easyMapsManager.setPreparedParameter(username);
//				List list = easyMapsManager.executeQuery(sql);
//				databaseAccess.close();	
//				return list;
//			}catch(Exception e) {
//				e.printStackTrace();
//			}
//			
//			return null;
//	 }
	 
	 public List getcityId(String cityname){
			String sql="select Id from dictionary where name=?;";
			try{
				DatabaseAccess databaseAccess = new DatabaseAccess(false);
				EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
				easyMapsManager.setPreparedParameter(cityname);
				List list = easyMapsManager.executeQuery(sql);
				databaseAccess.close();
				return list;
			}catch(Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	 
	 public boolean updatemembers(String name,String sex,int city,
	   		  int province,String email,String bank_card,int id){
		  DatabaseAccess dao=new DatabaseAccess();
		  Calenda C=new Calenda();
		  String now=C.getNowTime().toString();
			String sql="update members set name=?,sex=?,city=?" +
					",province=?,email=?,bank_card=?,uptime=? where Id=?;";
			System.out.println(name);
			dao.setPreparedParameter(name);
			dao.setPreparedParameter(sex);
			dao.setPreparedParameter(city);
			dao.setPreparedParameter(province);
			dao.setPreparedParameter(email);
			dao.setPreparedParameter(bank_card);
			dao.setPreparedParameter(now);
			dao.setPreparedParameter(id);
			boolean b=dao.executeUpdate(sql);
			System.out.println(b);
			return b;
	  }
	 public boolean getzg(double zg, int member_id){
		    DatabaseAccess dao=new DatabaseAccess();
			String sql="update members set yongjin=yongjin-? where Id=?;";
			dao.setPreparedParameter(zg);
			dao.setPreparedParameter(member_id);
			boolean b=dao.executeUpdate(sql);
			return b;
	 }
	 
	 public double getyj(int member_id){
		 String sql="select yongjin from members where Id=?;";
		 System.out.println(member_id);
			try{
				DatabaseAccess databaseAccess = new DatabaseAccess(false);
				EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
				easyMapsManager.setPreparedParameter(member_id);
				List list = easyMapsManager.executeQuery(sql);
				HashMap adu=(HashMap)list.get(0);
				double yongjin=Double.parseDouble((adu.get("yongjin").toString()));
				databaseAccess.close();
				return yongjin;
			}catch(Exception e) {
				e.printStackTrace();
			}
			return 0.0;
	 }
	 
	 public boolean verifyBusinessCode(String userid,String vcode) {
			// TODO Auto-generated method stub
			DatabaseAccess dao=new DatabaseAccess();
			String sql="select recognise_id from members where Id=?";
			String code="";
			
			try{
				DatabaseAccess databaseAccess = new DatabaseAccess(false);
				EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
				easyMapsManager.setPreparedParameter(userid);
				List list = easyMapsManager.executeQuery(sql);
				
				if(list.size()>0){
					HashMap adu=(HashMap)list.get(0);
					
					if(adu.get("recognise_id")==null){
						
					}
					else{
						code=adu.get("recognise_id").toString();
					}
					databaseAccess.close();	
						
				}
				if(code.equals(vcode)){
					String sql1="update members set is_recognise=1 where Id=?";
					dao.setPreparedParameter(userid);
					dao.executeUpdate(sql1);
					return true;
				}
				else{
					return false;
				}
				
			}catch(Exception e) {
				e.printStackTrace();
			}
			
			return false;
		}
}
